#include "weigen34.h"

/**
 * @brief WG34_Funtion功能函数，返回8位数据中1的个数
 * @param  num: 数据
 * @retval 
 */
uint8_t WG34_Funtion(uint8_t num)
{
	uint8_t i,n=0;
	for(i=0;i<8;i++)
	{
		if(num&(0x80>>i))
		{
			n++;
		}
	}
	return n;
}


/**
 * @brief WG34_WG0_PIN 控制韦根34，WG0的输出
 * @param  i: 0为低电平 ，其他为高电平
 * @retval 
 */
void WG34_WG0_PIN(uint8_t i)
{
	if(i == 0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_4);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_4);
	}
}

/**
 * @brief WG34_WG1_PIN 控制韦根34，WG1的输出
 * @param  i: 0为低电平 ，其他为高电平
 * @retval 
 */
void WG34_WG1_PIN(uint8_t i)
{
	if(i == 0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_5);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_5);
	}
}


/**
 * @brief WG34_init 韦根34初始化
 * @retval 无
 */
void WG34_init(void)
{
	GPIO_InitTypeDef GPIO_InitTypeture;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
	
	GPIO_InitTypeture.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitTypeture.GPIO_OType = GPIO_OType_PP;
	GPIO_InitTypeture.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
	GPIO_InitTypeture.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitTypeture.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_Init(GPIOA,&GPIO_InitTypeture);
	
	WG34_WG0_PIN(1);
	WG34_WG1_PIN(1);
}

/**
 * @brief WG34_OutPut
 * @param  uid: 要发送的uid数据
 * @retval 
 */
//暂时用于发送4字节uid
void WG34_OutPut(uint8_t *uid,uint8_t cln)
{
    unsigned char  i = 0;
    unsigned char  check_odd; //奇校验
    unsigned char  odd;     //填到韦根数组的奇校验位
    unsigned char  check_even; //偶校验
    unsigned char  even;	//填到韦根数组的偶校验位
    unsigned char  wg_data[4];
    unsigned char  byte = 0;//韦根字节
    unsigned char  bite = 0; //韦根比特
	
	for(i=0;i<4;i++)
	{
		wg_data[i] = uid[3-i];//将uid数据放入到wg_data中
	}
	
	//进行前16位的偶校验	取第1字节第8位右移至个位
	//求wg_data[0]和wg_data[1]
	check_even = WG34_Funtion(wg_data[0])+WG34_Funtion(wg_data[1]);
	//取个位数判断奇偶
    ((check_even & 0x01) == 1) ? (even = 1) : (even = 0);//1的个数为偶数时为0
	
	//进行后16位的奇校验
	check_odd = WG34_Funtion(wg_data[2])+WG34_Funtion(wg_data[3]);
	((check_odd & 0x01) == 1) ? (odd = 0) : (odd = 1); //1的个数为偶数时为0
	
	if(even)
	{
		WG34_WG1_PIN(0);
		Delay_50us(2);
		WG34_WG1_PIN(1);
	}
	else
	{
		WG34_WG0_PIN(0);
		Delay_50us(2);
		WG34_WG0_PIN(1);
	}
	Delay_50us(30);
	
	//发送32bite韦根数据
	for(byte = 0;byte<4;byte++)
	{
		for(bite=0;bite<8;bite++)
		{
			if(wg_data[byte]&(0x80>>bite))
			{
				WG34_WG1_PIN(0);
				Delay_50us(2);
				WG34_WG1_PIN(1);
			}
			else
			{
				WG34_WG0_PIN(0);
				Delay_50us(2);
				WG34_WG0_PIN(1);
			}
			Delay_50us(30);
		}
	}
	
	if(odd)
	{
		WG34_WG1_PIN(0);
		Delay_50us(2);
		WG34_WG1_PIN(1);
	}
	else
	{
		WG34_WG0_PIN(0);
		Delay_50us(2);
		WG34_WG0_PIN(1);
	}
	Delay_ms(70);//给下一次发送的间隔,半ms
}


/**
* @brief WG26_OutPut 26发送
 * @param  uid: uid数据
 * @param  cln:级联
 * @retval 
 */
void WG26_OutPut(uint8_t *uid,uint8_t cln)
{
	unsigned char  i = 0;
    unsigned char  check_odd; //奇校验
    unsigned char  odd;     //填到韦根数组的奇校验位
    unsigned char  check_even; //偶校验
    unsigned char  even;	//填到韦根数组的偶校验位
    unsigned char  wg_data[3];
    unsigned char  byte = 0;//韦根字节
    unsigned char  bite = 0; //韦根比特
	uint8_t u[4];
	for(i=0;i<4;i++)
	{
		u[i] = uid[3-i];
	}
	memcpy(wg_data,&u[1],3);
	
	//处理获得偶偶校验的数据
	u[0] = wg_data[0];
	u[1] = wg_data[1]&0xf0;
	u[2] = wg_data[1]&0x0f;
	u[3] = wg_data[2];
	//进行前16位的偶校验	取第1字节第8位右移至个位
	//求u[0]和u[1]
	check_even = WG34_Funtion(u[0])+WG34_Funtion(u[1]);
	//取个位数判断奇偶
    ((check_even & 0x01) == 1)?(even = 1) : (even = 0);//1的个数为偶数时为0
	
	//进行后16位的奇校验
	check_odd = WG34_Funtion(u[2])+WG34_Funtion(u[3]);
	((check_odd & 0x01) == 1) ? (odd = 0) : (odd = 1); //1的个数为偶数时为1
	
	if(even)
	{
		WG34_WG1_PIN(0);
		Delay_50us(2);
		WG34_WG1_PIN(1);
	}
	else
	{
		WG34_WG0_PIN(0);
		Delay_50us(2);
		WG34_WG0_PIN(1);
	}
	Delay_50us(30);
	
	
	//发送26bite韦根数据
	for(byte = 0;byte<3;byte++)
	{
		for(bite=0;bite<8;bite++)
		{
			if(wg_data[byte]&(0x80>>bite))
			{
				WG34_WG1_PIN(0);
				Delay_50us(2);
				WG34_WG1_PIN(1);
			}
			else
			{
				WG34_WG0_PIN(0);
				Delay_50us(2);
				WG34_WG0_PIN(1);
			}
			Delay_50us(30);
		}
	}
	
	
	if(odd)
	{
		WG34_WG1_PIN(0);
		Delay_50us(8);
		WG34_WG1_PIN(1);
	}
	else
	{
		WG34_WG0_PIN(0);
		Delay_50us(8);
		WG34_WG0_PIN(1);
	}
	Delay_ms(70);//给下一次发送的间隔,半ms
}


